import { login, logout, getInfo } from '@/api/user'
import { getToken, removeToken, UserType } from '@/utils/auth'
//import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    // commit('SET_TOKEN', 'admin-token')
    // setToken('admin-token')
    return new Promise((resolve, reject) => {
      login({ account: username.trim(), password: password })
        .then((data) => {
          commit('SET_TOKEN', data.token)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    // commit('SET_NAME', 'test')
    // commit('SET_AVATAR', 'avatar')
    // commit('SET_ROLES', ['admin'])
    // return { roles: ['admin'] }

    return new Promise((resolve, reject) => {
      getInfo()
        .then((response) => {
          let data = response.data
          if (!data) {
            return reject('验证失败, 请重新登录')
          }
          const { username, userType } = data
          commit('SET_NAME', username)
          commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80')
          let roles = []
          if (userType === UserType.USER.typeCode) {
            roles = [UserType.USER.des]
          } else if (userType === UserType.SUPER_ADMIN.typeCode) {
            roles = [UserType.SUPER_ADMIN.des]
          } else if (userType === UserType.ADMIN.typeCode) {
            roles = [UserType.ADMIN.des]
          } else if (userType === UserType.EMPTY.typeCode) {
            roles = [UserType.EMPTY.des]
          }
          commit('SET_ROLES', roles)
          resolve({ roles })
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeToken() // must remove  token  first
          //resetRouter()
          commit('RESET_STATE')
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    removeToken() // must remove  token  first
    commit('RESET_STATE')
    // return new Promise((resolve) => {
    //   removeToken() // must remove  token  first
    //   commit('RESET_STATE')
    //   resolve()
    // })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
